﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using BlogApp.Data.Service;
using BlogApp.WP8.Resources;
using BlogApp.WP8.Utils;
using BlogApp.WP8.ViewModel;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Practices.ServiceLocation;

namespace BlogApp.WP8
{
    public partial class CommentPage : PhoneApplicationPage
    {
        private CommentViewModel _vm;
        private string _blogPostId;

        public CommentPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.AppBarButtonTextSend;
            _blogPostId = ServiceLocator.Current.GetInstance<PostViewModel>().SelectedBlogPost.Id.ToString();
            _vm = (CommentViewModel)DataContext;

            _vm.PropertyChanged += OnPropertyChanged;

            if (e.NavigationMode == NavigationMode.Back)
            {
                if (PhoneApplicationService.Current.State.ContainsKey("name"))
                    NameInputTextBox.Text = PhoneApplicationService.Current.State["name"].ToString();

                if (PhoneApplicationService.Current.State.ContainsKey("email"))
                    MailInputTextBox.Text = PhoneApplicationService.Current.State["email"].ToString();

                if (PhoneApplicationService.Current.State.ContainsKey("comment"))
                    CommentInputTextBox.Text = PhoneApplicationService.Current.State["comment"].ToString();
            }
            else
            {
                PhoneApplicationService.Current.State.Clear();
                _vm.Comment = _vm.UserName = _vm.MailAdress = string.Empty;
            }

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            _vm.PropertyChanged -= OnPropertyChanged;
            
            PhoneApplicationService.Current.State["name"] = NameInputTextBox.Text;
            PhoneApplicationService.Current.State["email"] = MailInputTextBox.Text;
            PhoneApplicationService.Current.State["comment"] = CommentInputTextBox.Text;


            base.OnNavigatedFrom(e);
        }


        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Success")
            {

                if (_vm.Success)
                {
                    MessageBox.Show(AppResources.CommentSentSuccess, string.Empty, MessageBoxButton.OK);
                    NameInputTextBox.Text = CommentInputTextBox.Text = MailInputTextBox.Text = string.Empty;
                }
                else
                {
                    MessageBox.Show(AppResources.CommentSentFail, string.Empty, MessageBoxButton.OK);
                }
            }
        }

        private async void AppBarIconButtonSend_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(NameInputTextBox.Text))
            {
                MessageBox.Show(AppResources.UserNameError);
                return;
            }

            if (string.IsNullOrEmpty(MailInputTextBox.Text) || MailInputTextBox.Text.IsNoValidMailAdress())
            {
                MessageBox.Show(AppResources.MailAdressError);
                return;
            }

            if (string.IsNullOrEmpty(CommentInputTextBox.Text))
            {
                MessageBox.Show(AppResources.CommentError);
                return;
            }


            // ToDo Fix this!
            _vm.Comment = CommentInputTextBox.Text;
            _vm.UserName = NameInputTextBox.Text;
            _vm.MailAdress = MailInputTextBox.Text;

            _vm.PostCommentToBlogPost.Execute(_blogPostId);
        }
    }
}